<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Session;
use think\session\driver\Redis;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {

        $weekdays = ['Sunday' => '星期日', 'Monday' => '星期一', 'Tuesday' => '星期二', 'Wednesday' => '星期三', 'Thursday' => '星期四', 'Friday' => '星期五', 'Saturday' => '星期六'];

        $url = "https://v1.yiketianqi.com/free/week?unescape=1&appid=53366126&appsecret=ZI333G1I&city=泰安";

        // 发送HTTP请求
        $weatherInfo = file_get_contents($url);
        
        // 解码JSON响应
        $weatherInfo = json_decode($weatherInfo, true);

        $last = [];
        for($i = 1; $i < 5;$i++) {
            $last[] = [
                'temperature' => $weatherInfo['data'][$i]['tem_night'].'-'.$weatherInfo['data'][$i]['tem_day'].'℃',
                'date' => $weekdays[date('l',strtotime('+'.$i.' day'))],
                'img' => $weatherInfo['data'][$i]['wea_img']
            ];
        }
        $weather = [
            'today' => [
                'date' => date('Y年m月d日'),
                'location_name' => '泰安良庄',
                'date_name' => $weekdays[date('l')],
                'weather' => $weatherInfo['data'][0]['wea'],
                'temperature' => $weatherInfo['data'][0]['tem_night'].'-'.$weatherInfo['data'][0]['tem_day'].'℃',
                'wind_south' => $weatherInfo['data'][0]['win_speed'],
                'rainfall' => mt_rand(0,99).'%',
                'humidity' => mt_rand(0,99).'%',
                'img' => $weatherInfo['data'][0]['wea_img']
            ],
            'last' => $last
        ];


        // // 获取蔬菜价格
        // $city = [
        //     [
        //         'name' => '上海',
        //         'url' => 'http://www.shveg.com/cn/price/'
        //     ],
        //     [
        //         'name' => '北京',
        //         'url' => 'http://www.xinfadi.com.cn/priceDetail.html'
        //     ],
        //     [
        //         'name' => '济南',
        //         'url' => 'http://www.sdprice.org.cn/f/number_price-vegetables19.html?pageNo=1'
        //     ],
        //     [
        //         'name' => '广州',
        //         'url' => 'https://121.8.226.252/basic/sendReportInfoes'
        //     ],
        //     [
        //         'name' => '黑龙江',
        //         'url' => 'http://www.shveg.com/cn/price/'
        //     ],
        // ];

        // $data = file_get_contents('http://www.shveg.com/cn/price/');
        // // 使用正则表达式匹配价格
        // // 假设价格格式是：<p class="price">$X.XX</p>
        
        // preg_match_all('/<font color="#1EA100">甘兰菜</font>/i', $data, $matches);
        
        // $prices = $matches[1]; // 价格数组
        // dump($matches);exit;
        
        // // 打印价格
        // foreach ($prices as $price) {
        //     echo $price, "\n";
        // }
        $redis = new \think\cache\driver\Redis();
        $jrxse = $redis->get('jrxse');
        $jrxsds = $redis->get('jrxsds');
        $zrjyzje = $redis->get('zrjyzje');
        $zrjyzzl = $redis->get('zrjyzzl');

        $this->view->assign('jrxse',$jrxse);
        $this->view->assign('jrxsds',$jrxsds);
        $this->view->assign('zrjyzje',$zrjyzje);
        $this->view->assign('zrjyzzl',$zrjyzzl);
        $this->view->assign('time', time());
        $this->view->assign('weather', $weather);
        return $this->view->fetch();
    }

    public function getCarsInNum() {
        // if($this->request->isPost()) {
            $carsInNum = Db::name("balance")->where(['Gross_weight_time1' => ['between',[date('Y-m-d 00:00:00'),date('Y-m-d 23:59:59')]]])->count();
            
            $serviceFeeAmount = Db::name("balance")->where(['Gross_weight_time1' => ['between',[date('Y-m-d 00:00:00'),date('Y-m-d 23:59:59')]]])->value(Db::raw('SUM(ServicePrice+HygienePrice)'));

            return json(['code' => 1,'msg' => '查询成功','data' => ['in_num' => $carsInNum,'service_fee_amount' => $serviceFeeAmount ]]);
        // }
    }

    public function video() {
        return $this->view->fetch();
    }

    public function getRdpClient() {
        $command = "start mstsc /v:111.36.145.250:3389 /u:Administrator /p:";
        exec($command);
    }

    public function getChartData() {
        if($this->request->isPost()) {
            $data = [
                'fiveData' => [
                    'day' => [date('m-d',strtotime('-4 day')),date('m-d',strtotime('-3 day')),date('m-d',strtotime('-2 day')),date('m-d',strtotime('-1 day')),date('m-d')],
                    'series' => [
                        [
                            'name' => '芹菜',
                            'type' => 'line',
                            'stack' => 'Total',
                            'data' => [8.60, 8.50, 8.80, 8.60, 8.80],
                            // 设置折线颜色
                            'lineStyle' => [
                                'color' => 'green', // 这里设置为红色
                                'width' => 2, // 设置线宽
                            ],
                            'itemStyle' =>[
                                'color' => 'green'
                            ],
                            'smooth' => true

                        ],
                        [
                            'name' => '小白菜',
                            'type' => 'line',
                            'stack' => 'Total',
                            'data' => [ 1.1, 1.00, 1.10, 1.20, 1.20],
                            // 设置折线颜色
                            'lineStyle' => [
                                'color' => 'yellow', // 这里设置为红色
                                'width' => 2, // 设置线宽
                            ],
                            'itemStyle' =>[
                                'color' => 'yellow'
                            ],
                            'smooth' => true

                        ],
                        [
                            'name' => '菠菜',
                            'type' => 'line',
                            'stack' => 'Total',
                            'data' => [6.00, 6.00, 6.2, 5.8, 6.00],
                            // 设置折线颜色
                            'lineStyle' => [
                                'color' => 'red', // 这里设置为红色
                                'width' => 2, // 设置线宽
                            ],
                            'itemStyle' =>[
                                'color' => 'red'
                            ],
                            'smooth' => true

                        ],
                        [
                            'name' => '韭菜',
                            'type' => 'line',
                            'stack' => 'Total',
                            'data' => [8.80, 8.80, 8.40, 8.60, 8.80],
                            // 设置折线颜色
                            'lineStyle' => [
                                'color' => '#7c8260', // 这里设置为红色
                                'width' => 2, // 设置线宽
                            ],
                            'itemStyle' =>[
                                'color' => '#7c8260'
                            ],
                            'smooth' => true

                        ],
                        [
                            'name' => '茄子',
                            'type' => 'line',
                            'stack' => 'Total',
                            'data' => [2.00, 1.60, 1.80, 1.70, 2.00],
                            // 设置折线颜色
                            'lineStyle' => [
                                'color' => 'orange', // 这里设置为红色
                                'width' => 2, // 设置线宽
                            ],
                            'itemStyle' =>[
                                'color' => 'orange'
                            ],
                            'smooth' => true

                        ],
                        [
                            'name' => '冬瓜',
                            'type' => 'line',
                            'stack' => 'Total',
                            'data' => [2.00, 2.00, 2.00, 2.00, 2.00],
                            // 设置折线颜色
                            'lineStyle' => [
                                'color' => '#EF9F9F', // 这里设置为红色
                                'width' => 2, // 设置线宽
                            ],
                            'itemStyle' =>[
                                'color' => '#EF9F9F'
                            ],
                            'smooth' => true

                        ],
                        [
                            'name' => '菜花',
                            'type' => 'line',
                            'stack' => 'Total',
                            'data' => [7.60, 7.60, 8.2, 7.80, 7.60],
                            // 设置折线颜色
                            'lineStyle' => [
                                'color' => '#930707', // 这里设置为红色
                                'width' => 2, // 设置线宽
                            ],
                            'itemStyle' =>[
                                'color' => '#930707'
                            ],
                            'smooth' => true

                        ],
                        [
                            'name' => '尖椒',
                            'type' => 'line',
                            'stack' => 'Total',
                            'data' => [5.6, 5.6, 5.8, 5.8, 6.00],
                            // 设置折线颜色
                            'lineStyle' => [
                                'color' => '#13A552', // 这里设置为红色
                                'width' => 2, // 设置线宽
                            ],
                            'itemStyle' =>[
                                'color' => '#13A552'
                            ],
                            'smooth' => true

                        ],
                        [
                            'name' => '黄瓜',
                            'type' => 'line',
                            'stack' => 'Total',
                            'data' => [3.4, 3.60, 3.4, 3.60, 3.60],
                            // 设置折线颜色
                            'lineStyle' => [
                                'color' => '#0F838E', // 这里设置为红色
                                'width' => 2, // 设置线宽
                            ],
                            'itemStyle' =>[
                                'color' => '#0F838E'
                            ],
                            'smooth' => true

                        ],
                    ],
                    'legend' => ['芹菜', '小白菜', '菠菜', '韭菜', '茄子','冬瓜','菜花','尖椒','黄瓜']
                            
                ],
                'saleNumData' => [
                    'day' => [date('m-d',strtotime('-4 day')),date('m-d',strtotime('-3 day')),date('m-d',strtotime('-2 day')),date('m-d',strtotime('-1 day')),date('m-d')],
                ],
            ];

            return json([
                'code' => 1,
                'msg' => '查询成功',
                'data' => $data
            ]);
        }
    }

    public function getVegetablePrice(){
        if($this->request->isPost()) {

        }
    }

    public function changeData() {
        $redis = new \think\cache\driver\Redis();
        if($this->request->isPost()){
            $data = $this->request->post();
            foreach($data as $k => $v){
                $redis->set($k,$v);
            }

            $jrxse = $redis->get('jrxse');
            $jrxsds = $redis->get('jrxsds');
            $zrjyzje = $redis->get('zrjyzje');
            $zrjyzzl = $redis->get('zrjyzzl');

            $this->view->assign('jrxse',$jrxse);
            $this->view->assign('jrxsds',$jrxsds);
            $this->view->assign('zrjyzje',$zrjyzje);
            $this->view->assign('zrjyzzl',$zrjyzzl);
            return  $this->view->fetch('test');
        }

        $jrxse = $redis->get('jrxse');
        $jrxsds = $redis->get('jrxsds');
        $zrjyzje = $redis->get('zrjyzje');
        $zrjyzzl = $redis->get('zrjyzzl');

        $this->view->assign('jrxse',$jrxse);
        $this->view->assign('jrxsds',$jrxsds);
        $this->view->assign('zrjyzje',$zrjyzje);
        $this->view->assign('zrjyzzl',$zrjyzzl);

        return  $this->view->fetch('test');
    }
}
